﻿function InitList() {    
    $("form")[0].submit();
}

function BindList(json) {
    var html = $("#tmpl_list").tmpl(FixJson(json));
    $("#tb_list").find("tr:not(.td_title)").remove();
    $("#tb_list").append(html).treeTable({ expandable: true, initialState: "expanded" });
}

function FixJson(json) {
    var arr = [];
    var aIdx = 0;
    var sIdx = 0;
    $.each(json.cityList, function (i, v) {
        var obj = {};
        obj.type = "c";
        obj.id = v.id;
        obj.name = v.name;
        obj.order = v.order;
        obj.pname = " ";
        obj.pid = " ";
        arr.push(obj);
        for (; aIdx < json.areaList.length; aIdx++) {
            if (json.areaList[aIdx].pid != v.id) {
                break;
            }
            obj = {};
            obj.type = "a";
            obj.id = json.areaList[aIdx].id;
            obj.name = json.areaList[aIdx].name;
            obj.order = json.areaList[aIdx].order;
            obj.pname = v.name;
            obj.pid = json.areaList[aIdx].pid;
            arr.push(obj);
            for (; sIdx < json.streetList.length; sIdx++) {
                if (json.streetList[sIdx].pid != json.areaList[aIdx].id) {
                    break;
                }
                obj = {};
                obj.type = "s";
                obj.id = json.streetList[sIdx].id;
                obj.name = json.streetList[sIdx].name;
                obj.order = json.streetList[sIdx].order;
                obj.pname = json.areaList[aIdx].name;
                obj.pid = json.streetList[sIdx].pid;
                arr.push(obj);
            }
        }
    });
    return arr;
}

function GetListItemBg() {
    GetListItemBg.index++;
    if (GetListItemBg.index % 2 == 0) {
        return "#ccc";
    }
    return "#fff";
}

GetListItemBg.index = 0;

function Edit(id, type) {
    var url = "$.htm?id=" + id;
    switch (type) {
        case "c":
            url = url.replace("$", "EditCity");
            break;
        case "a":
            url = url.replace("$", "EditArea");
            break;
        case "s":
            url = url.replace("$", "EditStreet");
            break;
    }
    window.location.replace(url);
}

function Delete(id, type) {
    $.ajax({
        url: "proxyforcity.aspx",
        dataType: "script",
        data: { m: "Delete", id: id, type: type },
        error: function () {
            alert("删除失败");
        }
    });
}

function GotoListPage() {
    window.location.replace("ListCityAreaStreet.htm");
}

function InitAddArea() {
    $.ajax({ url: "../system_position/Proxy.ashx", type: "post", dataType: "json",
        data: { method: "listCity" }
        , success: function (json) {
            $("#ddl_City").append("#full_City", json.listCity);
        }
    });

    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

function InitAddCity() {   

    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

// 绑定城区列表
function BindCity() {
    $.ajax({ url: "../system_position/Proxy.ashx", type: "post", dataType: "json",
        data: { method: "listCity" }
        , success: function (json) {
            $("#ddl_City").empty().append("#full_City", json.listCity);
            CitySelected();
        }
    });
}

// 城区列表选择时绑定地区列表
function CitySelected() {
    var cityId = $("#ddl_City ").val();
    
    $.ajax({ url: "../system_position/Proxy.ashx", type: "post", dataType: "json",
        async: false,
        data: { method: "listAreaByCity", cityId: cityId }
        , success: function (json) {
            $("#ddl_Area").empty().append("#full_Area", json.listAreaByCity);
        }
    });
}

function InitAddStreet() {
    BindCity();

    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

function InitEditAreaPage(json) {
    $(":input[name=init]").val(0);
    $(":input[name=name]").val(json.name);
    $(":input[name=order]").val(json.order);
    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

function InitEditArea() {
    var id = QueryString("id");
    $(":input[name=id]").val(id);
    $("form")[0].submit();
}

function InitEditCityPage(json) {
    $(":input[name=init]").val(0);
    $(":input[name=name]").val(json.name);
    $(":input[name=order]").val(json.order);
    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

function InitEditCity() {
    var id = QueryString("id");
    $(":input[name=id]").val(id);
    $("form")[0].submit();
}

function InitEditStreetPage(json) {
    $(":input[name=init]").val(0);
    $(":input[name=name]").val(json.name);
    $(":input[name=order]").val(json.order);
    $("#btnSave").click(function () {
        $("form")[0].submit();
    });
}

function InitEditStreet() {
    var id = QueryString("id");
    $(":input[name=id]").val(id);
    $("form")[0].submit();
}